package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    /**
     * 根据菜品id查询数据
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        DishVO dishVo = new DishVO();
        Dish byId = dishMapper.getById(id);
        BeanUtils.copyProperties(byId,dishVo);
        List<DishFlavor> byDishId = dishFlavorMapper.getByDishId(id);
        dishVo.setFlavors(byDishId);
        return dishVo;
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    //开启事务，保证原子性，因为要操作两张表
    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        //插入菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        //插入口味表数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());



        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();

        return new PageResult(total,result);
    }

    /**
     * 批量删除菜品数据
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {

        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == 1){
                //起售中
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        List<Long> setmealIds = setmealDishMapper.getSetmealDishByDishId(ids);

        if (setmealIds != null && setmealIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品数据
        dishMapper.deleteByid(ids);

        //删除口味数据
        dishFlavorMapper.deleteByDishIds(ids);

    }

    /**
     * 修改菜品数据
     * @param dishDTO
     */
    @Override
    @AutoFill(OperationType.UPDATE)
    @Transactional
    public void update(DishDTO dishDTO) {

        Dish dish = new Dish();
            //修改菜品数据
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //删除口味数据
        dishFlavorMapper.deleteByDishId(dish.getId());
        if (flavors != null && flavors.size() > 0 ){
            flavors.forEach(flavor -> flavor.setDishId(dish.getId()));
            //插入口味数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品起售、停售
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    /**
     * 根据菜品类型查询菜品数据
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getByTypeId(Integer categoryId) {
        List<Dish> dishList = dishMapper.getByTypeId(categoryId);
        return dishList;
    }
    /**
     * 根据菜品类型查询菜品数据和口味数据
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> getDishFlavorByTypeId(Integer categoryId) {
        List<DishVO> dishVOList = new ArrayList<>();
        List<Dish> dishList = dishMapper.getByTypeId(categoryId);
        for (Dish dish : dishList) {
            if (dish.getStatus() == StatusConstant.DISABLE){continue;}
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(dish.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
